package com.getpebble.android.firmware;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.common.model.FirmwareVersion;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.common.model.PblDeviceJoinManifestModel;
import com.getpebble.android.common.model.PblDeviceModel;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.framework.firmware.FirmwareManifestBundle;
import com.getpebble.android.framework.firmware.FirmwareManifestBundle.ReleaseType;
import java.lang.ref.WeakReference;

public class FirmwareUpdateCheckTask extends PblAsyncTask {
    public static final String TAG = FirmwareUpdateCheckTask.class.getSimpleName();
    private FirmwareManifestBundle mBundle;
    private WeakReference<Context> mContext;
    private PblDevice mDevice;
    private FirmwareVersion mDeviceFirmwareVersion;
    private boolean mIsRecovery = false;
    private Listener mUpdateFoundListener;

    public interface Listener {
        void onFirmwareUpdateCheckComplete(PblDevice pblDevice, FirmwareManifestBundle firmwareManifestBundle);

        void onInRecoveryMode(PblDevice pblDevice);
    }

    public FirmwareUpdateCheckTask(Context context, PblDevice device, FirmwareVersion deviceFirmwareVersion, Listener updateFoundListener) {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        } else if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        } else if (deviceFirmwareVersion == null) {
            throw new IllegalArgumentException("'deviceFirmwareVersion' cannot be null!");
        } else if (updateFoundListener == null) {
            throw new IllegalArgumentException("'listener' cannot be null!");
        } else {
            this.mContext = new WeakReference(context);
            this.mDevice = device;
            this.mDeviceFirmwareVersion = deviceFirmwareVersion;
            this.mUpdateFoundListener = updateFoundListener;
        }
    }

    private Context getContext() {
        if (this.mContext != null) {
            return (Context) this.mContext.get();
        }
        return null;
    }

    public boolean doInBackground() {
        Trace.debug(TAG, "Starting update check task");
        Context context = getContext();
        if (context == null) {
            Trace.error(TAG, "Context is null; failing task");
            return false;
        }
        this.mIsRecovery = getIsRecovery(context);
        this.mBundle = getInstallableBundle(context);
        return true;
    }

    private boolean getIsRecovery(Context context) {
        PblDeviceRecord record = PblDeviceModel.getPblDeviceRecord(context.getContentResolver(), this.mDevice);
        if (record != null) {
            return record.isRunningRecoveryFw;
        }
        Trace.warning(TAG, "record is null; returning getIsRecovery = false");
        return false;
    }

    private FirmwareManifestBundle getInstallableBundle(Context context) {
        FirmwareManifestBundle installableBundle = null;
        if (context == null) {
            Trace.error(TAG, "getInstallableBundle: Context was null");
            return null;
        }
        Cursor cursor = PblDeviceJoinManifestModel.getFirmwareForDevice(context.getContentResolver(), this.mDevice.getAddress(), new PblPreferences(context).getStringData(PrefKey.RELEASE_CHANNEL, ReleaseType.RELEASE_V2.getPath()));
        if (cursor.getCount() == 0) {
            Trace.info(TAG, "No firmware found");
        }
        if (cursor != null) {
            while (cursor.moveToNext()) {
                ContentValues contentValues = new ContentValues();
                DatabaseUtils.cursorRowToContentValues(cursor, contentValues);
                FirmwareManifestBundle bundle = FirmwareManifestBundle.fromContentValues(contentValues);
                if (this.mDeviceFirmwareVersion.compareTo(bundle.getNormalMetadata().getFriendlyVersion()) < 0) {
                    installableBundle = bundle;
                }
            }
            cursor.close();
        }
        return installableBundle;
    }

    public void onTaskSuccess() {
        if (this.mIsRecovery) {
            this.mUpdateFoundListener.onInRecoveryMode(this.mDevice);
        } else {
            this.mUpdateFoundListener.onFirmwareUpdateCheckComplete(this.mDevice, this.mBundle);
        }
    }

    public void onTaskFailed() {
    }
}
